package code;

import java.util.Scanner;
/**
 * 
 * @author
 * 
 * This class represents Tree Object
 */
public class Tree {
	private TreeNode root;
	private boolean sessionEnd = false;//if session is end
	public Tree() {
		this.root = new TreeNode();
	}

	public Tree(TreeNode root) {
		this.root = root;
	}

	/**
	 * 
	 * @param label          the label for new node
	 * @param prompt		 the prompt for new node
	 * @param message		 the message for new node
	 * @param parentLabel    the parent label of new node
	 * @return   true if the new node is added successfully and false otherwise
	 */
	public boolean addNode(String label, String prompt, String message, String parentLabel) {
		TreeNode node = getNodeReference(parentLabel);
		TreeNode newNode = new TreeNode();
		newNode.setLabel(label);
		newNode.setMessage(message);
		newNode.setPrompt(prompt);
		if (node.getLeft() == null) {
			node.setLeft(newNode);
			return true;
		}
		if (node.getMiddle() == null) {
			node.setMiddle(newNode);
			return true;
		}
		if (node.getRight() == null) {
			node.setRight(newNode);
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @param label  the label by which to find the node reference
	 * @return  the node reference found by the label
	 */
	public TreeNode getNodeReference(String label) {
		TreeNode temp = root;
		if (temp != null) {
			if (label.equals(temp.getLabel())) {
				return temp;
			}
			if (temp.getLeft() != null) {
				TreeNode t = new Tree(temp.getLeft()).getNodeReference(label);
				if (t != null) {
					return t;
				}
			}
			if (temp.getMiddle() != null) {
				TreeNode t = new Tree(temp.getMiddle()).getNodeReference(label);
				if (t != null) {
					return t;
				}
			}
			if (temp.getRight() != null) {
				TreeNode t = new Tree(temp.getRight()).getNodeReference(label);
				if (t != null) {
					return t;
				}
			}
			return null;
		} else {
			return null;
		}
	}

	/**
	 * This method is for traveling the tree in pre order
	 */
	public void preOrder() {
		TreeNode temp = root;
		if (temp != null) {
			System.out.println(root);
		}
		if (temp.isLeaf()) {
			return;
		}
		if (temp.getLeft() != null) {
			new Tree(temp.getLeft()).preOrder();
		}
		if (temp.getMiddle() != null) {
			new Tree(temp.getMiddle()).preOrder();
		}
		if (temp.getRight() != null) {
			new Tree(temp.getRight()).preOrder();
		}
	}

	/**
	 * This method handles the help session
	 */
	public void beginSession() {
		System.out.println();
		System.out.println("Help Session Starting...");
		questionAndAnswer();
	}

	/**
	 * This mothod will call itself recursively and if the session is end it will return immediately
	 */
	private void questionAndAnswer() {
		TreeNode temp = root;
		if (temp != null) {
			System.out.println(temp.getMessage());
			if (temp.isLeaf()) {
				System.out.println();
				System.out.println("Thank you for using this automated help service!");
				sessionEnd = true;
				return;
			} else {
				Scanner scanner = new Scanner(System.in);
				if(temp.getLeft()!=null){
					System.out.println("1 " + temp.getLeft().getPrompt());
				}
				if(temp.getMiddle()!=null){
					System.out.println("2 " + temp.getMiddle().getPrompt());
				}
				if(temp.getRight()!=null){
					System.out.println("3 " + temp.getRight().getPrompt());
				}
				System.out.println("0 Exit Session.");
				String line = scanner.nextLine();
				
				if(line.equals("1")){
					if(temp.getLeft()!=null){
						 new Tree(temp.getLeft()).questionAndAnswer();
						 if(sessionEnd){
							 return;
						 }
					}
				}
				if(line.equals("2")){
					if(temp.getMiddle()!=null){
						 new Tree(temp.getMiddle()).questionAndAnswer();
						 if(sessionEnd){
							 return;
						 }
					}
				}
				if(line.equals("3")){
					if(temp.getRight()!=null){
						 new Tree(temp.getRight()).questionAndAnswer();
						 if(sessionEnd){
							 return;
						 }
					}
				}
				if(line.equals("0")){
					sessionEnd = true;
				}
			}
		}
	}
}
